package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.wan37.client.handler.ClientHandler;
import com.wan37.client.service.SkillService;
import com.wan37.client.service.cache.RoleServiceCache;
import com.wan37.client.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.AllRoleLabel;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.Game;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : luoyong
 * @date : 2020-07-01 16:09
 **/
@Component(PtoNum.SGAMEAOIRESPONSE + "")
public class SGameAoiResponse implements ReceiveMsg {

    private final static Logger logger = LoggerFactory.getLogger(SGameAoiResponse.class);
    private ClientHandler clientHandler = ClientHandler.getInstance();
    @Autowired
    private UserServiceCache userServiceCache;
    @Autowired
    private RoleServiceCache roleServiceCache;

    @Override
    public void execute(Message msg) {
        Game.SGameAoiResponse sGameAoiResponse = (Game.SGameAoiResponse) msg;
        int code = sGameAoiResponse.getCode();
        String content = sGameAoiResponse.getContent();
        switch (code) {
            case Common.GAME_AOI_SUCCESS:
            case Common.GAME_BOSS_SCENE_SUCCESS:
                //更新全部实体
                clientHandler.updateAllAOIAllRole(sGameAoiResponse.getAllRoleList());
                //切换场景，显示全部实体之后，进行原先实体技能对应优先级别队列map的清空，避免内存泄漏
                SkillService.clearAllRoleIdToPBQMap();
                /**
                 * 获取场景下的全部实体的技能以及初始化技能对应的可以释放的优先级技能队列
                 */
                if (!ClientHandler.allRoleMap.isEmpty()) {
//                    clientHandler.sendSkillMessage(code, ClientHandler.allRoleMap.keySet().stream().collect(Collectors.toList()));
                    for (GameEntity.AllRole allRole : ClientHandler.allRoleMap.values()) {
                        if (allRole.getLabel() == AllRoleLabel.MONSTER) {
                            //初始化实体可以释放的技能队列
                            SkillService.addAllRoleIdToPBQMap(allRole.getAllRoleId());
                        }
                    }
                }
                break;
            case Common.GAME_AOI_ERROR:
                logger.info("请求场景出错！！！", content);
                break;
            default:
                logger.info("错误的 Code, code : {}", code);
                break;
        }
    }
}
